import type { UserConfigExport, ConfigEnv } from 'vite';
import type { ElckerViteConfig } from './build/types';

import { resolve } from 'path';
const pathResolve = (dir: string): any => {
  return resolve(__dirname, '.', dir);
};

import ViteConfig from './build/vite/ViteConfigs';
import { defineConfig } from 'vite';

export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  const root = process.cwd();
  const Config: ElckerViteConfig = ViteConfig({ command, mode });
  return defineConfig({
    root,
    logLevel: Config.LogLevel,
    resolve: {
      alias: [
        {
          find: '@',
          replacement: pathResolve('src'),
        },
        {
          find: '~',
          replacement: pathResolve('build'),
        },
        // /#/xxxx => types/xxxx
        {
          find: '#',
          replacement: pathResolve('src/types'),
        },
        {
          find: '#B',
          replacement: pathResolve('build/types'),
        },
        // ['@vue/compiler-sfc', '@vue/compiler-sfc/dist/compiler-sfc.esm-browser.js'],
        {
          find: '_c',
          replacement: pathResolve('src/components'),
        },
        // mock
        {
          find: '_m',
          replacement: pathResolve('mock'),
        },
      ],
    },
    css: Config.css,
    plugins: Config.Plugin,
    server: Config.server,
    build: {
      // Turning off brotliSize display can slightly reduce packaging time
      brotliSize: false,
      chunkSizeWarningLimit: 5000,
      terserOptions: {
        compress: {
          keep_infinity: true,
          // Used to delete console in production environment
          drop_console: false,
        },
      },
    },
  });
};
